package com.xxl.job.admin.controller;

import com.xxl.job.admin.core.cron.CronExpression;
import com.xxl.job.admin.core.model.JobTimingStrategy;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.util.UserUtil;
import com.xxl.job.admin.dao.JobTimingStrategyDao;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author  songsong
 * @date  2022/11/28 10:12
 * @version 1.0
 * 定时策略
**/
@Controller
@RequestMapping("/jobTimingStrategy")
public class JobTimingStrategyController {

    @Resource
    JobTimingStrategyDao dao;
    @Resource
    XxlJobService xxlJobService;

    @RequestMapping
    public String index(Model model) {
//        return "jobTimingStrategy/list";
        return "jobTimingStrategy/jobTimingStrategy.index";
    }

    /**
     * 添加定时策略
     *
     * @param timingStrategy {@link JobTimingStrategy}
     * @return {@link ReturnT}
     */
    @RequestMapping("/save")
    @ResponseBody
    public ReturnT<String> save(HttpServletRequest request,JobTimingStrategy timingStrategy) {
        if(checkCron(timingStrategy.getCron())){
            timingStrategy.setAddUser(UserUtil.getUsername(request));
            timingStrategy.setAddTime(new Date());
            return new ReturnT(String.valueOf(dao.save(timingStrategy)));
        }
        return ReturnT.error("cron非法");
    }

    @RequestMapping("/update")
    @ResponseBody
    public ReturnT<String> update(HttpServletRequest request,JobTimingStrategy timingStrategy) {
        if(checkCron(timingStrategy.getCron())){
            timingStrategy.setEditUser(UserUtil.getUsername(request));
            timingStrategy.setEditTime(new Date());
            xxlJobService.updateNextTriggerTime(timingStrategy);
            return ReturnT.ok("更新成功");
        }
        return ReturnT.error("cron非法");
    }

    @RequestMapping("/remove")
    @ResponseBody
    public ReturnT<String> remove(int id) {
        List<XxlJobInfo> job = xxlJobService.getAllJobByScheduleConf(id + "");
        if (!CollectionUtils.isEmpty(job)) {
            return ReturnT.error("此定时策略已被任务使用，不允许删除！");
        }
        dao.delete(id);
        return ReturnT.SUCCESS;
    }



    /**
     * 根据条件查询定时策略列表
     * @return
     */
    @RequestMapping("/findQuartzListByPage")
    @ResponseBody
    Map<String, Object>  findQuartzListByPage(@RequestParam(required = false, defaultValue = "0") int start, @RequestParam(required = false, defaultValue = "10") int length) {
        List<JobTimingStrategy> list = dao.pageList(start, length);
        int list_count = dao.pageListCount();
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("recordsTotal", list_count);		// 总记录数
        maps.put("recordsFiltered", list_count);	// 过滤后的总记录数
        maps.put("data", list);
        return  maps;
    }

    /**
     * 查询定时策略列表
     *
     */
    @RequestMapping("/findQuartzList")
    @ResponseBody
    ReturnT<List<JobTimingStrategy>> findQuartzList() {
        List<JobTimingStrategy> list = dao.list();
        return ReturnT.ok(list);
    }

    boolean checkCron(String cron) {
        return CronExpression.isValidExpression(cron);
    }
}

